package cn.tedu.csms.job.service.impl;

import cn.tedu.csms.base.exception.ServiceException;
import cn.tedu.csms.base.response.StatusCode;
import cn.tedu.csms.job.mapper.JobMapper;
import cn.tedu.csms.job.pojo.dto.EmpeLoginQuery;
import cn.tedu.csms.job.pojo.dto.JobSaveParam;
import cn.tedu.csms.job.pojo.entity.Job;
import cn.tedu.csms.job.pojo.vo.JobVO;
import cn.tedu.csms.job.service.JobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class JobServiceImpl implements JobService {
    @Autowired
    JobMapper jobMapper;
    @Override
    public JobVO login(EmpeLoginQuery empeLoginQuery) {
        log.debug("用户登录业务，参数={}", empeLoginQuery);
        JobVO jobVO=jobMapper.selectByUsername(empeLoginQuery.getUsername());
        if(jobVO==null){
            throw  new ServiceException(StatusCode.USERNAME_ERROR);
        }else if(!jobVO.getPassword().equals(empeLoginQuery.getPassword())){
            throw  new ServiceException(StatusCode.PASSWORD_ERROR);
        }else{
            return jobVO;
        }
    }

    @Override
    public void saveJob(JobSaveParam jobSaveParam) {
        log.debug("新增员工工作信息业务，参数={}", jobSaveParam);
        Job job=new Job();
        BeanUtils.copyProperties(jobSaveParam,job);
        if(job.getId()==null){
            job.setPassword("12345");
            job.setCreateTime(new Date());
            jobMapper.insert(job);
        }else{
            job.setUpdateTime(new Date());
            jobMapper.update(job);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("删除员工工作信息，参数={}",id);
        jobMapper.deleteById(id);
    }

    @Override
    public List<JobVO> select(EmpeLoginQuery empeLoginQuery) {
        log.debug("加载工作信息业务：{}",empeLoginQuery);
        List <JobVO>list=jobMapper.select(empeLoginQuery);
        return list;
    }

    @Override
    public void updateStatus(Integer id, String status) {
        log.debug("修改员工状态业务：{}",id,status);
        Job job=new Job();
        job.setId(id);
        job.setStatus(status);
        jobMapper.update(job);
    }

    @Override
    public List<JobVO> selectEmpesByShopId(Long shopId) {
        log.debug("获取店铺员工信息业务：{}",shopId);
        EmpeLoginQuery empeLoginQuery = new EmpeLoginQuery();
        empeLoginQuery.setShopId(shopId);
        List<JobVO> jobVOS = jobMapper.select(empeLoginQuery);
        if(jobVOS!=null){
            return jobVOS;
        }
        return Collections.emptyList();
    }
}
